from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.views.generic import View,TemplateView
from django.contrib.auth.models import User,Group


def login(request,*args,**kwargs):
    a=  User.objects.filter()
    return render(request,'login.html')

class LoginView(View):
    def get(self,request,*args,**kwargs):
        return render(request, 'login.html')

class LoginViewV2(TemplateView):
    template_name = "login.html"

from django.core import serializers
from django.http import Http404
class GroupListView(View):
    def get(self,request,*args,**kwargs):
        queryset = Group.objects.all()
        return HttpResponse(serializers.serialize("json",queryset),content_type="application/json")

class GroupMembersView(View):
        def get_queryset(self):
            groupObj = self.get_group_obj()
            return groupObj.user_set.all()

        def get_group_obj(self):
            try:
                groupObj = Group.objects.get(name=self.request.GET.get("name"))
            except Group.DoesNotExist:
                return Http404
            except Group.MultipleObjectsReturned:
                return Http404
            return groupObj

        def get(self,request,*args,**kwargs):
            queryset = self.get_queryset()
            return HttpResponse(serializers.serialize("json", queryset), content_type="application/json")

class UserGroupsView(View):
    def get_queryset(self):
        userObj = self.get_user_obj()
        return userObj.groups.all()

    def get_user_obj(self):
        try:
            userObj = User.objects.get(username=self.request.GET.get("username"))
        except User.DoesNotExist:
            return Http404
        except User.MultipleObjectsReturned:
            return Http404
        return userObj

    def get(self,request,*args,**kwargs):
        queryset = self.get_queryset()
        return HttpResponse(serializers.serialize("json",queryset),content_type="application/json")

from django.http import QueryDict
class UserGroupManagerView(View):
    def get_user_obj(self):
        try:
            userObj = User.objects.get(username=QueryDict(self.request.body).get("username"))
        except User.DoesNotExist:
            return Http404
        except User.MultipleObjectsReturned:
            return Http404
        return userObj

    def get_group_obj(self):
        try:
            groupObj = Group.objects.get(name=QueryDict(self.request.body).get("groupname"))
        except Group.DoesNotExist:
            return Http404
        except Group.MultipleObjectsReturned:
            return Http404
        return groupObj


    def delete(self,request,*args,**kwargs):
        '''
        将用户从指定组中删除
        :param request:
        :param args:
        :param kwargs:
        :return:
        '''
        groupObj = self.get_group_obj()
        userObj = self.get_user_obj()
        groupObj.user_set.remove(userObj)
        return HttpResponse()

    def put(self,request,*args,**kwargs):
        '''
        将用户添加到组
        :param request:
        :param args:
        :param kwargs:
        :return:
        '''
        groupObj = self.get_group_obj()
        userObj = self.get_user_obj()
        groupObj.user_set.add(userObj)
        return HttpResponse()